//
//  PrepareFlyVC.m
//  Wisdom
//
//  Created by apple on 2018/3/15.
//  Copyright © 2018年 DJI. All rights reserved.
//

#import "PrepareFlyVC.h"
#import <DJISDK/DJISDK.h>
#import <DJIUILibrary/DJIUILibrary.h>
#import "AppDelegate.h"
#import "AirCraftVC.h"
#import "DJIRootViewController.h"

#define ENTER_DEBUG_MODE 0
@interface PrepareFlyVC ()<DJISDKManagerDelegate>
@property(strong, nonatomic)  UILabel  *version;
@property(strong, nonatomic)  UILabel  *registered;
@property(strong, nonatomic)  UIButton *registerBtn;
@property(strong, nonatomic)  UILabel  *connected;
@property(strong, nonatomic)  UIButton *connectBtn;
@end

@implementation PrepareFlyVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor lightGrayColor];
    [self registerWithProduct];
    [self initUI];
}

- (void)initUI {
    //注册
    self.registered = [UILabel new];
    self.registered.text = @"尚未注册";
    self.registered.textAlignment = NSTextAlignmentCenter;
    self.registered.backgroundColor = [UIColor blackColor];
    self.registered.textColor = [UIColor whiteColor];
    self.registered.font = [UIFont systemFontOfSize:18.f];
    [self.view addSubview:self.registered];
    [self.registered mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(20);
        make.size.mas_equalTo(CGSizeMake(100, 50));
    }];
    
    //注册
    self.registerBtn = [UIButton new];
    [self.registerBtn setTitle:@"注册" forState:0];
    self.registerBtn.titleLabel.font = [UIFont systemFontOfSize:18.f];
    self.registerBtn.backgroundColor = [UIColor blackColor];
    [self.registerBtn addTarget:self action:@selector(registerButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.registerBtn setTitleColor:[UIColor whiteColor] forState:0];
    [self.view addSubview:self.registerBtn];
    [self.registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(80);
        make.size.mas_equalTo(CGSizeMake(100, 50));
    }];
    
    //登陆状态
    self.connected = [UILabel new];
    self.connected.text = @"尚未登陆";
    self.connected.textAlignment = NSTextAlignmentCenter;
    self.connected.textColor = [UIColor whiteColor];
    self.connected.backgroundColor = [UIColor blackColor];
    self.connected.font = [UIFont systemFontOfSize:18.f];
    [self.view addSubview:self.connected];
    [self.connected mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(140);
        make.size.mas_equalTo(CGSizeMake(100, 50));
    }];
    
    self.connectBtn = [UIButton new];
    [self.connectBtn setTitle:@"登陆" forState:0];
    self.connectBtn.titleLabel.font = [UIFont systemFontOfSize:18.f];
    self.connectBtn.backgroundColor = [UIColor blackColor];
    [self.connectBtn addTarget:self action:@selector(connectButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.connectBtn setTitleColor:[UIColor whiteColor] forState:0];
    [self.view addSubview:self.connectBtn];
    [self.connectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(200);
        make.size.mas_equalTo(CGSizeMake(100, 50));
    }];
    
    //进入飞控画面
    UIButton *jump2FlyControlBtn = [UIButton new];
    [jump2FlyControlBtn setTitle:@"进入飞行控制页面" forState:0];
    jump2FlyControlBtn.titleLabel.font = [UIFont systemFontOfSize:18.f];
    jump2FlyControlBtn.backgroundColor = [UIColor redColor];
    [jump2FlyControlBtn addTarget:self action:@selector(jump2flyConreol:) forControlEvents:UIControlEventTouchUpInside];
    [jump2FlyControlBtn setTitleColor:[UIColor whiteColor] forState:0];
    [self.view addSubview:jump2FlyControlBtn];
    [jump2FlyControlBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(260);
        make.height.mas_equalTo(50);
    }];
    
    //返回上一页
    UIButton *backBtn = [UIButton new];
    [backBtn setTitle:@"返回上一页" forState:0];
    backBtn.titleLabel.font = [UIFont systemFontOfSize:18.f];
    backBtn.backgroundColor = [UIColor redColor];
    [backBtn addTarget:self action:@selector(backBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [backBtn setTitleColor:[UIColor whiteColor] forState:0];
    [self.view addSubview:backBtn];
    [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
        make.height.mas_equalTo(50);
    }];
    
    //地图选点
    UIButton *selMapBtn = [UIButton new];
    [selMapBtn setTitle:@"进入地图选点页面" forState:0];
    selMapBtn.titleLabel.font = [UIFont systemFontOfSize:18.f];
    selMapBtn.backgroundColor = [UIColor redColor];
    [selMapBtn addTarget:self action:@selector(selMapBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [selMapBtn setTitleColor:[UIColor whiteColor] forState:0];
    [self.view addSubview:selMapBtn];
    [selMapBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(320);
        make.height.mas_equalTo(50);
    }];
    
}

/**
 *地图选点
 */
- (void)selMapBtnClicked:(UIButton *)sender {
    DJIRootViewController *vc = [DJIRootViewController new];
    [self.navigationController pushViewController:vc animated:NO];
}

/**
 *进入飞控画面
 */
- (void)jump2flyConreol:(UIButton *)sender {
    AirCraftVC *vc = [AirCraftVC new];
    [self.navigationController pushViewController:vc animated:NO];
}

/**
 *返回
 */
- (void)backBtnClicked:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)registerButton:(UIButton *)sender {
    // [self registerWithProduct];
}

- (void)connectButton:(UIButton *)sender {
       [self connectToProduct];
}

- (void) registerWithProduct
{
    [DJISDKManager registerAppWithDelegate:self];
}

- (void) connectToProduct
{
    if (ENTER_DEBUG_MODE) {
        NSString *debugIP = @"Please enter your Debug IP address here.";
        NSLog(@"Connecting to Product using debug IP address: %@",debugIP);
        [DJISDKManager enableBridgeModeWithBridgeAppIP:debugIP];
    }else
    {
        NSLog(@"Connecting to product...");
        [DJISDKManager startConnectionToProduct];
    }
}

#pragma mark - DJISDKManager Delegate Methods
- (void)appRegisteredWithError:(NSError *)error
{
    if (error == nil){
        
        NSLog(@"Registration Succeeded");
        [self connectToProduct];
        self.registered.text = @"注册成功";
        self.registerBtn.hidden = YES;
        
    }else
    {
        NSLog(@"Error Registrating App: %@", error.description);
        self.registered.text = @"注册失败";
        self.registerBtn.hidden = NO;
    }
}

- (void)productConnected:(DJIBaseProduct *)product
{
    if (product != nil) {
        NSLog(@"Connection to new product successed!");
        self.connected.text = @"链接成功";
        self.connectBtn.hidden = YES;
    }
    
    //If this demo is used in China, it's required to login to your DJI account to activate the application. Also you need to use DJI Go app to bind the aircraft to your DJI account. For more details, please check this demo's tutorial.
    [[DJISDKManager userAccountManager] logIntoDJIUserAccountWithAuthorizationRequired:NO withCompletion:^(DJIUserAccountState state, NSError * _Nullable error) {
        if (error) {
            NSLog(@"Login failed: %@", error.description);
        }
    }];
}

- (void)productDisconnected{
    
    NSLog(@"Disconnected from product!");
    self.connected.text = @"NO";
    self.connectBtn.hidden = NO;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

-(void)dealloc {
    
}

@end
